package com.xiaoyao.novel.crawl;

import java.io.IOException;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.util.List;

import org.apache.log4j.Logger;

import com.xiaoyao.novel.parser.QidianParser;
import com.xiaoyao.novel.pojo.Book;
import com.xiaoyao.novel.pojo.Chapter;
import com.xiaoyao.novel.util.ClassUtil;
import com.xiaoyao.novel.util.HttpClientUtil;
import com.xiaoyao.novel.util.HttpUtil;

public class QidianCrawl extends BaseCrawl {

	private static HttpUtil httpBook = new HttpUtil("UTF-8");
	private static HttpUtil httpGBKBook = new HttpUtil("GBK");
	private static Logger logger = Logger.getLogger(QidianCrawl.class);

	public QidianCrawl() {
		tag = Book.TYPE_QIDIAN;
	}	
	static{
		try {
			HttpUtil.trustAllHttpsCertificates();
		} catch (KeyManagementException e) { 
			e.printStackTrace();
		} catch (NoSuchAlgorithmException e) { 
			e.printStackTrace();
		}
	}

	/**
	 * 爬一本书,目录
	 * 
	 * @param bookId
	 */
	public void crawlBook(Book book) {
		String page = "";
		try {
			//http://www.qidian.com/Book/2988814.aspx
//			if (ClassUtil.isEmpty(book.authorName)) {
//				String describUrl ="http://www.qidian.com/Book/" + book.webId + ".aspx";
//				logger.info("读取简介:" + describUrl);
//				page = httpBook.getPage(describUrl); 
//				QidianParser.parseDescrib(page, book);
//			}
			
			String categoryUrl = "https://book.qidian.com/info/"+book.webId ;;
			logger.info("读取目录:" + categoryUrl);
			page = httpBook.getPage(categoryUrl);
			List<Chapter> list = QidianParser.parseBook(page, book);
			for (int i = book.savedCount; i < list.size(); i++) {
				Chapter chapter = book.list.get(i);
				crawlChapter(chapter);
			}
			 
		} catch (IOException e) {
			logger.error(e);
		}
	}

	/**
	 * 爬章节类容
	 * 
	 * @param chapter
	 * @throws IOException
	 */
	public void crawlChapter(Chapter chapter) throws IOException {
		String urlStr =  chapter.chapterUrl;
		if (!urlStr .startsWith("http:")) {
			urlStr=	"https:" + urlStr ;
		}
		
		logger.info("爬取章节 :[" + urlStr + "]");
		String page = "";
		for (int i = 0; i < 3 && ClassUtil.isEmpty(page); i++) {
			try {
				page = httpBook.getPage(urlStr);
			} catch (Exception e) {
				// e.printStackTrace();
				logger.error(e);
			}
		} 
		QidianParser.parseChapter(page, chapter);
		chapter.chapterContent =filter (chapter.chapterContent); 
		if  (ClassUtil.isEmpty( chapter.chapterContent) ){
			urlStr = chapter.chapterTxtUrl;
			page = httpGBKBook.getPage(urlStr);
			page = filter(page);
			chapter.chapterContent = page; 
		}
		logger.info("内容长度:" + chapter.chapterContent.length() );
	}

	// ///////////////////////////////////////////
	// ///////////////////////////////////////////
	/**
	 * 过滤
	 * 
	 * @param page
	 * @return
	 */
	public String filter(String page) {
		page = page.replaceFirst("document.write\\('", "");
		page = page.replaceAll("<a href=https://www.qidian.com>.+", "");
		page = page.replaceAll("qidian", "tianfengfeiye");
		page = page.replaceAll("起点", "tianfengfeiye");
		page = page.replaceAll("<script.+?</script>", "");
		// 其他替换
		return page;
	}

	public static void main(String[] args) {
		Book book = new Book();
		book.webId = "1004595892";
		QidianCrawl crawl = new QidianCrawl();
		crawl.crawlBook(book);
	}

}
